package com.zw.yzk.kabu.data.repository;


import android.content.Context;

import com.zw.yzk.kabu.data.cache.store.UserStore;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.network.entity.UserEntity;
import com.zw.yzk.kabu.data.network.service.WechatLoginService;
import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.data.network.task.WechatLoginTask;

import javax.inject.Inject;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by zhanwei on 2016/12/24.
 */

public class WechatLoginRepository extends CommonRepository {

    @Inject
    UserStore userStore;
    private Context context;
    @Inject
    public WechatLoginRepository(Context context) {
        this.context = context;
    }

    @Override
    protected Observable connectServer(Task task) {
        WechatLoginService loginService = ServiceGenerator.createService(WechatLoginService.class);
        WechatLoginTask loginTask = (WechatLoginTask) task;

        return loginService.login(loginTask.code, loginTask.state);
    }

    @Override
    protected Func1<UserEntity, Observable> getServerData() {
        return new Func1<UserEntity, Observable>() {
            @Override
            public Observable call(UserEntity entity) {
                userStore.loginUser(entity, context);
                return Observable.just(true);
            }
        };
    }

}
